﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.Core
{
    public class PizzaTemplateVM : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        PizzaTemplate pizzaTemplate;
        public PizzaTemplate WrappedPizzaTemplate
        {
            get { return pizzaTemplate; }
        }

        public PizzaTemplateVM(PizzaTemplate pizzaTemplate)
        {
            this.pizzaTemplate = pizzaTemplate;
            pizzaTemplate.PropertyChanged += pizzaTemplate_PropertyChanged;
            this.isActive = pizzaTemplate.IsActive;
        }

        //Forward accessor
        public int ID
        {
            get { return pizzaTemplate.ID; }
        }
        //Forward accessor
        public string Name
        {
            get { return pizzaTemplate.Name; }
        }

        void pizzaTemplate_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "ID":
                case "Name":
                    OnPropertyChanged(e.PropertyName);
                    break;
                case "IsActive":
                    IsActive = pizzaTemplate.IsActive;
                    break;
            }
        }
        private bool isActive;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (value != isActive)
                {
                    isActive = value;
                    OnPropertyChanged("IsActive");
                }
            }
        }
        public static string GetToppingsDescription(IPizzaTemplate pizzaTemplate)
        {
            return String.Join(", ", pizzaTemplate.Toppings);
        }
        public string ToppingsDescription
        {
            get
            {
                return GetToppingsDescription(pizzaTemplate);
            }
        }
        public void UpdateModel()
        {
            pizzaTemplate.IsActive = this.IsActive;
        }
    }
}
